package com.settlement.system.controller;

import com.settlement.system.common.annotation.PreventDuplicateSubmit;
import com.settlement.system.common.result.Result;
import com.settlement.system.model.entity.SysColumnMatchCondition;
import com.settlement.system.model.form.ColumnMatchConditionForm;
import com.settlement.system.service.SysColumnMatchConditionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 字段对应条件 前端控制器
 * </p>
 *
 * @author huangrensen
 * @since 2023-09-19
 */
@RestController
@RequestMapping("/api/v1/columnMatchCondition")
@RequiredArgsConstructor
public class SysColumnMatchConditionController {

    private final SysColumnMatchConditionService sysColumnMatchConditionService;

    @Operation(summary = "保存字段对应条件", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:columnMatchCondition:save')")
    @PreventDuplicateSubmit
    public Result<?> saveColumnMatch(@Valid @RequestBody ColumnMatchConditionForm conditionForm) {
        return Result.success(sysColumnMatchConditionService.saveForm(conditionForm));
    }

    @Operation(summary = "获取字段对应条件", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping(value = "/list/{matchId}/{sourceType}")
    public Result<?> getConditionList(@Parameter(description = "关系ID") @PathVariable String matchId,
                                      @Parameter(description = "来源") @PathVariable String sourceType) {
        List<SysColumnMatchCondition> conditions = sysColumnMatchConditionService.getConditionList(matchId);
        return Result.success(conditions);
    }

    @Operation(summary = "删除字段对应条件", security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:columnMatchCondition:delete')")
    public Result<?> deleteCondition(@Parameter(description = "字段对应条件ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = sysColumnMatchConditionService.deleteCondition(ids);
        return Result.judge(result);
    }
}
